#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>
#include <errno.h>
#include <sys/poll.h>
#include <sys/epoll.h>

#define MYPORT 10001
#define MAXLINE 1024

#define MAX_CONNECT 10

//采用select单线程模式


int main(){
    //服务器初始化过程
    int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(listen_fd < 0){
        perror("socket()");
        return 1;
    }
    struct sockaddr_in seraddr;
    memset(&seraddr, 0, sizeof(seraddr));
    seraddr.sin_family = AF_INET;
    seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    seraddr.sin_port = htons(MYPORT);
    if(bind(listen_fd, (struct sockaddr*)&seraddr, sizeof(seraddr)) < 0){
        perror("bind()");
        return 1;
    }
    if(listen(listen_fd,3) < 0){
        perror("listen()");
        return 1;
    }

    //存放客户端地址
    socklen_t cli_len;
    struct sockaddr_in cliaddr;

    //连接用fd
    int connfd;
    int nfds;    
    //epoll描述符
    int epollfd;
    //epoll 事件
    struct epoll_event ev, events[MAX_CONNECT];

    epollfd = epoll_create1(0);
    if(epollfd == -1){
        perror("create()");
        exit(-1);
    }

    ev.events = EPOLLIN;
    ev.data.fd = listen_fd;
    if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_fd, &ev) == -1){
        perror("ctl()");
        exit(-1);
    }

    while(1){
        nfds = epoll_wait(epollfd, events, MAX_CONNECT, -1);

        if(nfds == -1){
            perror("wait()");
            exit(-1);
        }

        for(int i = 0; i < nfds; i++){
            if(events[i].data.fd == listen_fd){
                cli_len = sizeof(cliaddr);
                connfd = accept(listen_fd, (struct sockaddr*)&cliaddr, &cli_len);
                char temp[256];
                printf("accept from %s, port %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, temp, cli_len), ntohs(cliaddr.sin_port));
                if(connfd < 0){
                    perror("accept()");
                    exit(1);
                }
                ev.data.fd = connfd;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &ev);
            }
            else if(events[i].events & EPOLLIN){
                char buf[MAXLINE];
                int count = read(events[i].data.fd, buf, MAXLINE);
                if(count < 0){
                    perror("read()");
                    close(events[i].data.fd);
                    epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
                }
                else if(count == 0){
                    close(events[i].data.fd);
                    epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
                }
                else{
                    write(events[i].data.fd, buf, count);
                }
            }
            else if(events[i].events & EPOLLERR){
                perror("some error");
                close(events[i].data.fd);
                epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
            }
        }
    }

    return 0;
}